﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace View.BookForms
{
    public partial class BookSearchForm : Form
    {
        BookEventArgs _properties;

        public BookSearchForm()
        {
            InitializeComponent();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Controls2Fields();
            OnSubmit();
            SafeClose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            SafeClose();
        }

        private void SafeClose()
        {
            _properties = null;
            Hide();
        }

        public void ShowDialog(BookEventArgs fields)
        {
            if (Visible) { return; }

            _properties = fields;
            Fields2Controls();
            ShowDialog();
        }

        private void Controls2Fields()
        {
            _properties.Author = textBoxAuthor.Text;
            _properties.Title = textBoxTitle.Text;
            _properties.Isbn = textBoxIsbn.Text;
            _properties.SimpleId = textBoxSimpleId.Text;
        }

        private void Fields2Controls()
        {
            textBoxAuthor.Text = _properties.Author;
            textBoxTitle.Text = _properties.Title;
            textBoxIsbn.Text = _properties.Isbn;
            textBoxSimpleId.Text = _properties.SimpleId;
        }

        public event EventHandler<BookEventArgs> Submit;

        private void OnSubmit()
        {
            if (Submit != null)
            {
                Submit(this, _properties);
            }
        }

        private void BookSearchForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

    }
}
